package com.netease.controller;

import com.huaban.analysis.jieba.JiebaSegmenter;
import com.netease.bo.UserDictBO;
import com.netease.service.impl.UserDictServiceImpl;
import com.netease.pojo.UserDict;
import com.netease.utils.JsonResult;
import com.netease.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author aochong
 * @description 用户词典api
 * @date 2023/4/3 21:43
 */
@Api(value = "自定义数据元相关接口", tags = {"自定义数据元相关接口"})
@RequestMapping("userDict")
@RestController
public class UserDictController {

    private JiebaSegmenter segmenter = new JiebaSegmenter();
    private static final String PATH = "modeling-service/src/main/resources/dicts/user.dict";

    public static final Integer COMMON_PAGE_SIZE = 10;

    @Autowired
    private UserDictServiceImpl userDictService;

    @ApiOperation(value = "用户词典插入接口", notes = "用户词典插入接口", httpMethod = "POST")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody UserDictBO userDictBO) {

        if (userDictBO == null){
            JsonResult.errorMsg("新增内容不允许为空！！！");
        }

        boolean isExists = userDictService.queryUserDictIsExists(userDictBO.getCstmDataElmnt());

        if (!isExists){
            UserDict userDict = new UserDict();
            userDict.setCstmDataElmnt(userDictBO.getCstmDataElmnt());
            userDict.setWordFrqncy(userDictBO.getWordFrqncy());
            userDict.setPartOfSpch(userDictBO.getPartOfSpch());

            userDictService.createUserDict(userDict);
            return JsonResult.ok("插入成功！");
        }else{
            return JsonResult.errorMsg("该条记录已存在！");
        }
    }

    @ApiOperation(value = "用户词典更新", notes = "用户词典更新", httpMethod = "POST")
    @PostMapping("/update")
    public JsonResult select(@RequestBody UserDictBO userDictBO) {

        if (userDictBO == null){
            JsonResult.errorMsg("更新内容不允许为空！！！");
        }

        userDictService.updateUserDict(userDictBO);

        return JsonResult.ok("ok");
    }

    @ApiOperation(value = "用户词典分页查询", notes = "用户词典分页查询", httpMethod = "GET")
    @GetMapping("/list")
    public JsonResult list(
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize
    ) {

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult grid = userDictService.queryPagedUserDict(page, pageSize);

        return JsonResult.ok(grid);
    }

    @ApiOperation(value = "单条用户词典查询", notes = "单条用户词典查询", httpMethod = "GET")
    @GetMapping("/select")
    public JsonResult select(@RequestParam String cstmDataElmnt) {

        //判断字段注释必须不能为空
        if (StringUtils.isBlank(cstmDataElmnt)) {
            return JsonResult.errorMsg("自定义数据元必须不为空！！！");
        }

        //查询该条分词日志是否存在
        boolean isExist = userDictService.queryUserDictIsExists(cstmDataElmnt);

        if (!isExist) {
            return JsonResult.errorMsg("该词典信息在数据库中不存在！！！");
        }

        return JsonResult.ok(userDictService.queryUserDict(cstmDataElmnt));
    }

}
